package com.yyp.car.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yyp.car.Iservice.IGroundService;
import com.yyp.car.Iservice.INoticeService;
import com.yyp.car.Iservice.IStudentTTService;
import com.yyp.car.Iservice.ITestTypeService;
import com.yyp.car.Iservice.IUserService;
import com.yyp.car.dto.MarksDto;
import com.yyp.car.dto.StudentDto;
import com.yyp.car.model.Ground;
import com.yyp.car.model.Notice;
import com.yyp.car.model.StudentTT;
import com.yyp.car.model.TestType;
import com.yyp.car.model.User;

@Controller("indexController")
@RequestMapping("/index")
public class IndexController {
	@Resource(name = "testTypeService")
	private ITestTypeService testTypeService;
	@Resource(name = "groundService")
	private IGroundService groundService;
	@Resource(name = "userService")
	private IUserService userService;
	@Resource(name = "studentTTService")
	private IStudentTTService istt;
	@Resource(name="noticeService")
	private INoticeService noticeService;
	
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	@RequestMapping("/jszksyy")
	public String jszksyy(Model model) {
		List<TestType> testTypes = testTypeService.listTestType();
		model.addAttribute("testTypes", testTypes);
		return "index/jszksyy";
	}

	@RequestMapping("/showTestMes")
	public String showTestMes(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user.getStudentMes().getTestPlace() != null && !"".equals(user.getStudentMes().getTestPlace())) {
			Ground ground = groundService.findGroundByid(Integer.parseInt(user.getStudentMes().getTestPlace()));
			model.addAttribute("ground", ground);
		}
		return "index/showTestMes";
	}
	@RequestMapping("/cjsc")
	public String cjsc(HttpSession session,Model model) {
		User user=(User)session.getAttribute("user"); 
		List<User> ss=userService.listStudentByTid(user.getId());
		List<StudentDto> lsd=new ArrayList<StudentDto>();
		Integer marks=null;
		for(User u:ss){
			marks=istt.getMarks(u.getId(), u.getTtid());
			StudentDto sd=new StudentDto();
			sd.setMarks(marks);
			sd.setIdCard(u.getUsername());
			sd.setName(u.getStudentMes().getName());
			sd.setSex(u.getStudentMes().getSex());
			sd.setTestType(u.getTestType());
			sd.setSid(u.getId());
			sd.setTtid(u.getTtid());
			lsd.add(sd);
		}
		model.addAttribute("students", lsd);
		return "index/cjsc";
	}
	
	@RequestMapping("/showMarks")
	public String showMarks(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		List<StudentTT> stts=istt.listMarksAndTtid(user.getId());
		List<MarksDto> lmd=new ArrayList<MarksDto>();
		String testType=null;
		for(StudentTT stt:stts)
		{
			MarksDto md=new MarksDto();
			testType=testTypeService.findTestType(stt.getTtid());
			md.setMarks(stt.getMarks());
			md.setTestType(testType);
			lmd.add(md);
		}
		model.addAttribute("marks", lmd);
		return "index/cjcx";
	}
	@RequestMapping("/noticeList")
	public String noticeLost(Model model) {
		List<Notice> ln=noticeService.listNotice();
		model.addAttribute("notices", ln);
		return "index/noticeList";
	}
	@RequestMapping("/showNotice/{id}")
	public String showNotice(@PathVariable int id,Model model) {
		Notice notice=noticeService.findNotice(id);
		model.addAttribute("notice", notice);
		return "index/showNotice";
	}
}
